/**
 * @author LKQ
 * @date 2021/11/30 21:39
 * @description 该方法i++,时间复杂度为O(n)
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,3};
        System.out.println(solution.searchInsert(nums, 2));

    }
    public int searchInsert(int[] nums, int target) {
        int i = 0, location = 0, n = nums.length;
        if (n==0 || target < nums[0]) {
            return 0;
        }
        if (target > nums[n-1]) {
            return n;
        }
        while (i < n) {
            if (nums[i]!=target) {
                if (target > nums[i] && target < nums[i+1]){
                    location = i+1;
                    break;
                }
                i++;
            }else{
                location = i;
                break;
            }
        }
        return location;
    }
}
